package com.zhuimeng.ossdemo.controller;

import com.zhuimeng.ossdemo.entry.UploadResult;
import com.zhuimeng.ossdemo.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("/pic")
@Controller
public class UploadController {
    @Autowired
    private FileUploadService fileUploadService;
    @PostMapping("/upload")
    @ResponseBody
    public UploadResult upload(@RequestParam("file")MultipartFile multipartFile){
        return fileUploadService.upload(multipartFile);
    }
    @RequestMapping("/download")
    @ResponseBody
    public void download(@RequestParam("fileName") String objectName, HttpServletResponse response) throws IOException {
        //通知浏览器以附件形式下载
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(objectName.getBytes(), "ISO-8859-1"));
        this.fileUploadService.down(response.getOutputStream(),objectName);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public UploadResult delete(@RequestParam("fileName") String objName){
        return fileUploadService.delete(objName);
    }
}
